package cn.vw.hi.commen.action;

import java.io.PrintWriter;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;

import cn.vw.hi.commen.manager.DicManager;
import cn.vw.hi.commen.model.Loads;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class DicAction extends ActionSupport {

	private DicManager dicManager=null;
	private String  provinceId;
	private String cityId;
	private String countiesId;
	private Loads loads=null;
	private String latlng=null;
	private String ids=null;
	private String searchStr=null;
	private int radius;
	@Override
	public String execute() throws Exception {
		return "success";
	}
	//获得省列表
	public String provinceList() throws Exception{
		List provinceList=dicManager.provinceList();
		JSONArray jsonArray=JSONArray.fromObject(provinceList);
		backText(jsonArray.toString());
		return "Data";
	}
	//获得市列表
	public String cityList() throws Exception{
		List cityList=dicManager.cityList(provinceId);
		JSONArray jsonArray=JSONArray.fromObject(cityList);
		backText(jsonArray.toString());
		return "Data";
	}
	//获得区列表
	public String countiesList() throws Exception{
		List cityList=dicManager.countiesList(cityId);
		JSONArray jsonArray=JSONArray.fromObject(cityList);
		backText(jsonArray.toString());
		return "Data";
	}
	//添加路
	public String addLoad() throws Exception{
		ActionContext.getContext().getSession().put("location", loads.getProvinceId()+","+loads.getCityId()+","+loads.getCountiesId());
		boolean isSuccess=dicManager.addLoad(loads);
		return "AddLoad";
	}
	//获得路名列表
	public String loadList() throws Exception{
		List loadsList=dicManager.loadsList(provinceId,cityId,countiesId);
		JSONArray jsonArray=JSONArray.fromObject(loadsList);
		backText(jsonArray.toString());
		return "Data";
	}
	//获得路名列表forjson
	public String loadListForJson() throws Exception{
		List loadsList=dicManager.loadsList(provinceId,cityId,countiesId);
		JSONArray jsonArray=new JSONArray();
		JSONObject json=new JSONObject();
		for(int i=0;i<loadsList.size();i++)
		{
			JSONObject temp=new JSONObject();
			Loads loads=(Loads)loadsList.get(i);
			temp.put("id", loads.getId());
			temp.put("name", loads.getLoadName());
			temp.put("opt", "<a href='dic!modifyPage.action?loads.id="+loads.getId()+"'>修改</a>&nbsp;&nbsp;<a href='javascript:deleteLoads("+loads.getId()+");'>删除</a>");
			jsonArray.add(temp);
		}
		json.put("rows", jsonArray);
		json.put("total", loadsList.size());
		backText(json.toString());
		return "Data";
	}
	//修改路名
	public String modifyPage() throws Exception{
		loads=dicManager.findLoads(loads.getId());
		return "ModifyPage";
	}
	//修改路名
	public String modifyLoads() throws Exception{
		@SuppressWarnings("unused")
		boolean isSuccess=dicManager.modifyLoads(loads);
		return "List";
	}
	//删除路
	public String deleteLoads() throws Exception{
		boolean isSuccess=dicManager.deleteLoads(ids);
		JSONObject json=new JSONObject();
		json.put("info", isSuccess);
		backText(json.toString());
		return "Data";
	}
	//获得路名列表by 经纬度
	public String loadListByLatLng() throws Exception{
		List list=dicManager.loadListByLatLng(latlng,searchStr,radius);
		JSONArray json=JSONArray.fromObject(list);
		backText(json.toString());
		return "Data";
	}
	//获得所有城市
	public String cityAllList()throws Exception{
		dicManager.getProvinceCityList();
		return "";
	}
	//返回文本
	public void backText(String data) throws Exception
	{
		ServletActionContext.getResponse().setContentType ("text/html;charset=utf-8");
		
		  PrintWriter out = ServletActionContext.getResponse().getWriter();
		  out.print(data);
		  out.flush();
		  out.close();
	}
	public DicManager getDicManager() {
		return dicManager;
	}
	public void setDicManager(DicManager dicManager) {
		this.dicManager = dicManager;
	}
	public String getProvinceId() {
		return provinceId;
	}
	public void setProvinceId(String provinceId) {
		this.provinceId = provinceId;
	}
	public String getCityId() {
		return cityId;
	}
	public void setCityId(String cityId) {
		this.cityId = cityId;
	}
	public String getCountiesId() {
		return countiesId;
	}
	public void setCountiesId(String countiesId) {
		this.countiesId = countiesId;
	}
	public Loads getLoads() {
		return loads;
	}
	public void setLoads(Loads loads) {
		this.loads = loads;
	}
	public String getLatlng() {
		return latlng;
	}
	public void setLatlng(String latlng) {
		this.latlng = latlng;
	}
	public String getIds() {
		return ids;
	}
	public void setIds(String ids) {
		this.ids = ids;
	}
	public String getSearchStr() {
		return searchStr;
	}
	public void setSearchStr(String searchStr) {
		this.searchStr = searchStr;
	}
	public int getRadius() {
		return radius;
	}
	public void setRadius(int radius) {
		this.radius = radius;
	}

	
}
